package vn.co.thn.web.hotvideo.common;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public abstract class ControllerBase extends HttpServlet {
	private RequestDispatcher mDispatcher;
	private HttpServletRequest mRequest;
	private HttpServletResponse mResponse;

	@Override
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
		super.init(config);
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.mRequest = request;
		this.mResponse = response;
		Get(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.mRequest = request;
		this.mResponse = response;
		Post(request, response);
	}

	private String getPath() {
		ServletContext context = this.getServletContext();
		return context.getInitParameter("View");
	}

	protected void Forward(String Path_view) throws ServletException, IOException {
		mDispatcher = getServletContext().getRequestDispatcher(getPath() + Path_view + ".jsp");
		mDispatcher.forward(mRequest, mResponse);
	}

	protected void Redirect(String Path_view) throws ServletException, IOException {
		this.mResponse.sendRedirect(Path_view);
	}

	protected abstract void Get(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;

	protected abstract void Post(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;
}
